<template>
  <div class="login">
    <div class="box">
      <div class="round">
        <div class="roundMin">
          <img src="@/assets/logo.svg" alt="" />
        </div>
      </div>
      <div class="inputBox">
        <el-form ref="ruleFormRef" :model="ruleFormModel" :rules="rules">
          <el-form-item label="账号" prop="username">
            <el-input
              :prefix-icon="UserFilled"
              v-model="ruleFormModel.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="ruleFormModel.password"
              placeholder="请输入密码"
              show-password
              @keyup.enter.prevent="submitForm()"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttonBox">
        <el-button size="large" type="primary" @click="submitForm()">登录</el-button>
        <el-button size="large" @click="submitFormB()">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { UserFilled, Lock } from '@element-plus/icons-vue'
import { loginClick } from '@/api/login'
import router from '@/router'
import { useCounterStore } from '@/stores/counter'
const store = useCounterStore()
const res = ref()
const ruleFormRef = ref()
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z1-9][a-zA-Z0-9]{3,9}$/,
      message: '账号长度4-10位汉字、字母、数字',
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^(\w){6,20}$/, message: '密码长度不能小于6位', trigger: 'blur' },
  ],
})
const ruleFormModel = ref({
  username: 'admin',
  password: '123456',
})

const submitForm = async () => {
  try {
    // 表单验证
    await ruleFormRef.value.validate()
    // 登录操作
    res.value = await loginClick({
      username: ruleFormModel.value.username,
      password: ruleFormModel.value.password,
    })
    // 存储token
    store.setToken(res.value.data.data.token)

    // 跳转页面
    router.replace('/home')
  } catch (error) {
    // 错误处理
    console.log('catch', error)
  }
}

function submitFormB() {
  ruleFormModel.value.username = ''
  ruleFormModel.value.password = ''
}
</script>

<style scoped lang="scss">
.login {
  height: 98vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 30%;
    aspect-ratio: 100/54;
    background: #fff;
    padding: 20px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border-radius: 5px;
    .round {
      width: 25%;
      aspect-ratio: 1/1;
      border-radius: 50%;
      background: #fff;
      position: absolute;
      top: -71px;
      left: 37%;
      display: flex;
      flex-wrap: wrap;
      align-content: center;
      justify-content: center;
      box-shadow: 0px 0px 20px #fff;
      .roundMin {
        width: 80%;
        aspect-ratio: 1/1;
        border-radius: 50%;
        background: #fff;
        overflow: hidden;
        .img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .inputBox {
      height: 30%;

      margin-top: 15%;
    }
    .buttonBox {
      display: flex;
      justify-content: center;
      margin-bottom: 8%;
    }
  }
}
</style>
